package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.base.ProcessChain;
import com.zmj.sy.mom.srv.aps.bean.entity.base.ProcessChainItem;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.processchain.ProcessChainAddReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.processchain.ProcessChainEditReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.processchain.ProcessChainListReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.processchain.ProcessChainListResVo;
import com.zmj.sy.mom.srv.aps.mapper.ProcessChainItemMapper;
import com.zmj.sy.mom.srv.aps.mapper.ProcessChainMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Comparator;

@RequiredArgsConstructor
@Service
public class ProcessChainService {

    private final ProcessChainMapper processChainMapper;
    private final ProcessChainItemMapper processChainItemMapper;


    @Transactional
    public void add(ProcessChainAddReqVo reqVo) {
        if (!StringUtils.hasText(reqVo.getMyCode())) {
            throw SyExceptionUtils.e("工序链编码不能为空");
        }

        Integer count = processChainMapper.lambdaQuery().eq(ProcessChain::getMyCode, reqVo.getMyCode()).count();
        if (count > 0) {
            throw new IllegalArgumentException("工序链已存在");
        }

        ProcessChain processChain = MkBeanUtils.copyProperties(reqVo, ProcessChain.class);
        processChainMapper.insert(processChain);
    }

    @Transactional
    public void delete(BaseIdReqVo reqVo) {
        Integer count = processChainItemMapper.lambdaQuery().eq(ProcessChainItem::getChainId, reqVo.getId()).count();
        if(count > 0){
            throw SyExceptionUtils.e("该工序链下存在工序项，不能删除");
        }

        processChainMapper.deleteById(reqVo.getId());
    }

    public BasePageResVo<ProcessChainListResVo> list(ProcessChainListReqVo reqVo) {
        Page<ProcessChain> page = processChainMapper.lambdaQuery()
                .like(StringUtils.hasText(reqVo.getMyCode()), ProcessChain::getMyCode, reqVo.getMyCode())
                .like(StringUtils.hasText(reqVo.getName()), ProcessChain::getName, reqVo.getName())
                .page(reqVo.toPage());
        page.getRecords().sort(Comparator.comparingInt(e -> e.getMyCode().length()));
        return BasePageResVo.of(page, ProcessChainListResVo.class);
    }

    @Transactional
    public void edit(ProcessChainEditReqVo reqVo) {
        if (!StringUtils.hasText(reqVo.getMyCode())) {
            throw SyExceptionUtils.e("工序链编码不能为空");
        }

        Integer count = processChainMapper.lambdaQuery()
                .eq(ProcessChain::getMyCode, reqVo.getMyCode())
                .ne(ProcessChain::getId, reqVo.getId())
                .count();
        if (count > 0) {
            throw new IllegalArgumentException("工序链已存在");
        }

        ProcessChain processChain = MkBeanUtils.copyProperties(reqVo, ProcessChain.class);
        processChainMapper.updateById(processChain);
    }
}
